import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rp/wait_task_count_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';

//任务待办
class ChildrenTabOne extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ChildrenTabOneState();
  }
}

class ChildrenTabOneState extends State<ChildrenTabOne>{
  bool show = true;
  WaitTaskCountDatas datas = WaitTaskCountDatas();

  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return LoadBuilder(
        visibility: show,
        builder: RefreshIndicator(
          child: ListView(
            children: <Widget>[
              ItemCounter("入库待办任务", datas.warehouseInQty, () {
                Navigator.pushNamed(context, '/invTaskWaitDetails');
              }, 0),
              ItemCounter("出库待办任务", datas.warehouseOutQty, () {
                Navigator.pushNamed(context, '/outTaskWaitDetails');
              }, 1),
              ItemCounter("移库待办任务", datas.warehouseMoveQty, () {
                Navigator.pushNamed(context, '/moveTaskWaitDetails');
              }, 1),
              ItemCounter("盘库待办任务", datas.warehouseCheckQty, () {
                Navigator.pushNamed(context, '/changeTaskWaitDetails');
              }, 1),
              ItemCounter("退库待办任务", datas.warehouseBackQty, () {
                Navigator.pushNamed(context, '/backTaskWaitDetails');
              }, 1),
            ],
          ),
          onRefresh: getData,
        ));
  }

  Future getData() async {
    show = true;
    InternetAPI.getInstance().get("/OMS//MiTaskApproval/GetStayTaskQtyList2",
        (data) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        setState(() {
          show = false;
          datas = WaitTaskCountDatas.fromJson(data);
        });
      });
    }, params: {
      'warehouseid': DataSingleton().warehouseChoice.id,
      'tenantId': DataSingleton().Data.tenantId,
      "workuserid": DataSingleton().Data.id,
          'operaterUserId': DataSingleton().Data.id,
          'operaterUserName': DataSingleton().Data.username,
    });
  }
}

class ItemCounter extends StatelessWidget {
  String name;
  int counter;
  Function onTap;
  double top;

  ItemCounter(this.name, this.counter, this.onTap, this.top);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.all(12),
        margin: EdgeInsets.only(top: top),
        child: Row(
          children: <Widget>[
            Expanded(
                child: Text(
              name,
              style: TextStyle(color: Color(0xff323232), fontSize: 16),
            )),
            Text("【${counter}】",
                style: TextStyle(color: Color(0xff00A5F1), fontSize: 16)),
            Container(
              width: 16,
              height: 10,
              child: Image.asset("assets/images/arrow.png"),
            )
          ],
        ),
      ),
      onTap: onTap,
    );
  }
}
